package shili;

/**
 * 懒汉模式
 */
class SingletonLazy{
    private static volatile SingletonLazy instance = null;
    private static Object locker = new Object();
    public static SingletonLazy getInstance(){
        //判定当前是否应该要加锁
        if(instance==null){
            synchronized (locker){
                if(instance==null){
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
    private SingletonLazy(){

    }
}
public class Single2 {
    public static void main(String[] args) {
        SingletonLazy s = SingletonLazy.getInstance();
        SingletonLazy s1 = SingletonLazy.getInstance();
        System.out.println(s1==s);
    }
}
